package com.example.hrandroidapp.common;

import java.lang.reflect.Field;
import java.util.List;
import java.util.regex.Pattern;

import android.util.Patterns;

public class StringUtil {

	
	public static final Pattern EMAIL_ADDRESS
    = Pattern.compile(
        "[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}" +
        "\\@" +
        "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" +
        "(" +
            "\\." +
            "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}" +
        ")+"
    );
	
    /**
     * Is the string null or empty "" or only space characters.
     * 
     * @param text
     * @return
     */
    public static boolean isNullOrEmpty(String text) {
        return text == null || "".equals(text.trim());
    }

    /**
     * Compare two strings.
     * 
     * @param s1
     * @param s2
     * @return
     */
    public static int compareString(String s1, String s2) {
        int result = 0;

        if (isNullOrEmpty(s1)) {
            if (isNullOrEmpty(s2)) {
                result = 0;
            } else {
                result = -1;
            }
        } else {
            if (isNullOrEmpty(s2)) {
                result = 1;
            } else {
                result = s1.compareTo(s2);
            }
        }

        return result;
    }
    
    public static String center(String s, int size) {
        return center(s, size, " ");
    }
    
    public static String center(String s, int size, String pad) {
        if (pad == null)
            throw new NullPointerException("pad cannot be null");
        if (pad.length() <= 0)
            throw new IllegalArgumentException("pad cannot be empty");
        if (s == null || size <= s.length())
            return s;

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < (size - s.length()) / 2; i++) {
            sb.append(pad);
        }
        sb.append(s);
        while (sb.length() < size) {
            sb.append(pad);
        }
        return sb.toString();
    }

    /**
     * Parse the list to string separated by the delimiter. If the object in list is null, will skip it. If the
     * fieldName is not null, will use the value of the field instead of the Object.toString(). Example: If Class person
     * has a "name" field. And the list is {[name="Bob", age="14"],[name="George", age="35"]}, if you pass "name" as
     * fieldName, then the return string will be "Bob,George".
     * 
     * @param list
     * @param delimiter
     * @param fieldName The field name of the Object in the list.
     * @return
     */
    public static String listToString(List list, String delimiter, String fieldName) {
        StringBuilder sb = new StringBuilder();

        if (list == null || list.isEmpty()) {
            return null;
        } else {
            for (int n = 0; n < list.size(); n++) {
                Object object = list.get(n);
                if (object != null) {

                    // Get String that will be appended.
                    String appended = null;
                    if (fieldName != null) {
                        try {
                            Field field = object.getClass().getDeclaredField(fieldName); // NoSuchFieldException
                            field.setAccessible(true);
                            appended = field.get(object).toString();
                        } catch (Exception e) {
                            // Nothing to do.
                        }
                    } else {
                        appended = object.toString();
                    }

                    // Append the String.
                    if (n == 0) {
                        sb.append(appended);
                    } else {
                        sb.append(delimiter + appended);
                    }
                }
            }

            return sb.toString();
        }
    }
    
    
	public static String convertStringArrayToString(String[] array)
	{
		String result = "";
		for(int i = 0; array != null && i < array.length ; i++)
		{
			result += array[i] + ",";
		}
		if(result.length() > 0)
		{
			result = result.substring(0, result.length()-1);
		}
		return result;
	}
	
	public static boolean isInteger(String input) {
		try {
			Integer.parseInt(input);	
		}catch(NumberFormatException e) {
			return false;
		}
		
		return true;
	}
	
	public static boolean isDouble(String input) {
		try {
			Double.parseDouble(input);	
		}catch(NumberFormatException e) {
			return false;
		}
		
		return true;
	}
	
	public static boolean isValidEmail(String input)
	{
		Pattern pattern = Patterns.EMAIL_ADDRESS;
	    return pattern.matcher(input).matches();
	}

	public static String padLeft(String str, Integer length, char toPadChar) {
		
		if(isNullOrEmpty(str))
			return str;
		
		 int toPadNum = length - str.length();
		 
		 if(toPadNum <= 0)
			 return str;
		 
		 String toPadStr = "";
		 for(int i = 0 ; i < toPadNum ; i++)
		 {
			 toPadStr += String.valueOf(toPadChar);
		 }
		 
		 return toPadStr + str;
	}
}
